package cloning.cci4.ch1;

import java.util.HashSet;
import java.util.Set;

/*
 * Implement an algorithm to determine if a string has all unique characters. 
 * What if you can not use additional data structures?
 */

public class Task_1_1
{

	public static void main(String[] args)
	{
		String positive = "abcde";
		String negative = "abcdee";
		String complex = "aabbccddee";
		
		System.out.println(positive + " = " + isCharUniqe(positive));
		System.out.println("- - - - - - - - - - - - -");
		System.out.println(negative + " = " + isCharUniqe(negative));
		System.out.println("- - - - - - - - - - - - -");
		System.out.println(complex + " = " + isCharUniqe(complex));
		System.out.println("==========================");
		
		System.out.println(positive + " = " + isCharUniqeSimple(positive));
		System.out.println(negative + " = " + isCharUniqeSimple(negative));
		System.out.println(complex + " = " + isCharUniqeSimple(complex));
	}

	
	private static boolean isCharUniqe(String string)
	{
		char[] charArray = string.toCharArray();
		
		for (int index = 0; index < charArray.length; index++)
		{
			char currentChar = charArray[index]; 
			
			for (int innerIndex = 0; innerIndex < charArray.length; innerIndex++)
			{
				if (index != innerIndex && currentChar == charArray[innerIndex])
				{
					System.out.println("non unique char is " + charArray[innerIndex]);
					return false;
				}
			}
		}
		
		
		return true;
	}
	
	private static boolean isCharUniqeSimple(String string)
	{
		char[] charArray = string.toCharArray();
		
		Set<Character> charCollection = new HashSet<Character>();
		
		for (int index = 0; index < charArray.length; index++)
		{
			charCollection.add(charArray[index]);
		}
		
		return charArray.length == charCollection.size();
	}
	
}


